//引入第三方express模块
const express=require("express");
//引入第三方路由器模块
const router=express.Router();
//引入连接池
const pool=require("../pool")

//创建路由，端口
router.get("/",(req,res)=>{
  if(!req.session.uid){
    res.send({code:0,msg:"请先登录"})
    return;
  }
  var user_productId=req.session.uid
  var sql="select * from user_product where user_productId=? "
  pool.query(sql,[user_productId],(err,result)=>{
    if(err) console.log(err);
    res.send(result)
  })
})

//买入
router.post('/add',(req,res)=>{
  if(!req.session.uid){
    res.send({code:0,msg:"请先登录"})
    return;
  }
  var user_productId=req.session.uid;
  var buyAccount=parseFloat(req.body.buyAccount);
  var pname=req.body.pname;
  var tdate=req.body.tdate;
  var date=0
  var hrate=parseFloat(req.body.hrate);
  switch(tdate){ 
    case "10天" :
        date=10;
        break;
    case "18天" :
        date=18;
        break;  
    case "1个月" :  
        date=30;
        break; 
    case "3个月" :  
        date=90;
        break;  
    case "6个月" :  
        date=180;
        break;   
    case "12个月" :  
        date=360;
        break;                    
  }
  console.log(req.body)
  var sql="insert into user_product values(null,?,now(),?,?,?,?)"
  pool.query(sql,[pname,date,hrate,buyAccount,user_productId],(err,result)=>{
    if(err) throw err;
    //console.log(result);
  })
  var sqltwo='insert into history values(null,now(),?,1,?,?)';
  pool.query(sqltwo,[pname,buyAccount,user_productId],(err,result)=>{
    if(err)throw err;
    console.log(result);
  })
})

//卖出
router.get('/sell',(req,res)=>{
  if(!req.session.uid){
    res.send({code:0,msg:"请先登录"})
    return;
  }
  var user_productId=req.session.uid;
  var userProductId=Number(req.query.userProductId);
  var buyAccount=req.query.buyAccount
  var pname=""
  var sql="select * from  user_product where userProductId=? and user_productId=?;"
  pool.query(sql,[userProductId,user_productId],(err,result)=>{
    if(err) throw err
    pname=result[0].productName;
    var sql="delete from user_product where userProductId=? and user_productId=?"
    pool.query(sql,[userProductId,user_productId],(err,result)=>{ 
      if(err) throw err
      if(result.affectedRows>0){
        res.send({code:1,msg:"卖出成功"})
      }
        var sqltwo='insert into history values(null,now(),?,0,?,?)';
        pool.query(sqltwo,[pname,buyAccount,user_productId],(err,result)=>{
          if(err)throw err;
        })
    })
    
  })
  
  //修改账户信息
  var sqlthree= "update useraccount set userAccount = userAccount+? where uid=?";
  pool.query(sqlthree,[buyAccount,user_productId],(err,result)=>{
    if(err) throw err
    console.log(result)
  })
   
})


//获取交易记录
router.get('/getRecord',(req,res)=>{
  if(!req.session.uid){
    res.send({code:0,msg:"请先登录"})
    return;
  }
  var uid=req.session.uid
  var sql ="select * from history where user_accountId=?"
  pool.query(sql,[uid],(err,result)=>{
    if(err) throw err
    console.log(result)
    res.send(result)
  })
})

module.exports=router;